#include <iostream>
#include <map>
using namespace std;
int n, p;
typedef long long LL;

// 判断合法字符
bool check(char ch)
{
    if(ch >= '0' && ch <= '9' || ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z') return true;
    return false;
}
int main()
{
    cin >> n >> p;
    map<string, int> m;

    for(int i = 0;i < n;i++)
    {
        string s; int x;
        cin >> s >> x;
        m[s] = x;
    }

    LL ret = 0;
    char ch;
    string t = "";

    while(scanf("%c", &ch) != EOF)
    {
        if(check(ch))
        {
            t += ch;
        }
        else
        {
            ret = (ret + m[t]) % p;
            t = "";
        }
    }

    cout << ret << endl;
    return 0;
}